package cn.tianyun.pethome.org.service.impl;

import cn.tianyun.pethome.basic.base.service.impl.BaseServiceImpl;
import cn.tianyun.pethome.org.domain.Department;
import cn.tianyun.pethome.org.mapper.DepartmentMapper;
import cn.tianyun.pethome.org.service.IDepartmentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Resource
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> buildTree() {
        List<Department> departments = departmentMapper.loadAll();
        ArrayList<Department> root = new ArrayList<>();
        for (Department department:
        departments) {
            if(department.getParent_id()==null){
                root.add(department);
            }else{
                for (Department father:
                departments) {
                    if(father.getId().equals(department.getParent_id())){
                        father.getChildren().add(department);
                    }
                }
            }
        }
        return root;
    }

}
